<?php

class Cart {


	/** **************************************** variables ****************************************/

	private $items = array();
	private $total_quantity = 0;

	private $sub_total,	$grand_total = 0.00;
	private $transport_price, $discount = -1;

	/** **************************************** end variables ****************************************/



	/** **************************************** public/protected functions ****************************************/
	
	public function get_grand_total() { return $this->grand_total; }

	public function get_sub_total() { return $this->sub_total; }

	public function get_total_quantity() { return $this->total_quantity; }

	public function get_items() { return $this->items; }

	public function get_transport() { return $this->transport_price; }

	public function get_discount() { return $this->discount; }

	public function add_product($product) {
		if (is_int($found_key = $this->search_cart_items($product))) {
			$this->items[$found_key]->quantity += 1;
			if ($this->items[$found_key]->mounting_price > -1) { $this->sub_total += $product->mounting_price; }
		}
		else { $found_key = array_push($this->items, Orderdetail::new_based_on($product)) - 1; }
		$this->total_quantity += 1;
		$this->sub_total += $product->price;
		$this->update_grand_total();
		return $found_key;
	}

	public function get_item_qtyw($found_key) {
		return $this->items[$found_key]->quantity;
	}

	public function remove_product($product) {
		if (is_int($found_key = $this->search_cart_items($product))) {
			if ($this->items[$found_key]->mounting_price > -1 ) { $this->sub_total -= $product->mounting_price; }
			if ($this->items[$found_key]->quantity > 1) { $this->items[$found_key]->quantity -= 1; }
			else { unset($this->items[$found_key]); }
			$this->total_quantity -= 1;
			if ($this->total_quantity === 0) { return $this->empty_all_items(); }
			$this->sub_total -= $product->price;
			$this->update_grand_total();

		}
	}

	public function add_mounting($product) {
		if (!is_int($found_key = $this->search_cart_items($product))) { $found_key = $this->add_product($product); }
		$this->items[$found_key]->mounting_price = $product->mounting_price;
		$this->sub_total += $this->items[$found_key]->quantity * $product->mounting_price;
		$this->update_grand_total();
	}

	public function remove_mounting($product) {
		if (is_int($found_key = $this->search_cart_items($product)) && ($this->items[$found_key]->mounting_price > -1)) {
			$this->sub_total -= $product->mounting_price * $this->items[$found_key]->quantity;
			$this->items[$found_key]->mounting_price = -1;
			$this->update_grand_total();
		}
	}

	public function update_transport($transport_price) {
		$this->transport_price = abs($transport_price);
		$this->update_grand_total();
	}

	public function remove_transport() {
		if ($this->transport_price > -1) {
			$this->transport_price = -1;
			$this->update_grand_total();
		}
	}

	public function update_discount($discount_amt) {
		$this->discount = abs($discount_amt);
		$this->update_grand_total();
	}

	public function remove_discount() {
		if ($this->discount > -1) {
			$this->discount = -1;
			$this->update_grand_total();
		}
	}


	public function empty_all_items() {
		$this->items = array();
		$this->total_quantity = 0;
		$this->grand_total = $this->sub_total = 0.00;
		$this->discount = $this->transport_price = -1;
	}

	/** **************************************** end public/protected functions ****************************************/



	/** **************************************** private functions ****************************************/

	private function search_cart_items($product) {
		foreach($this->items as $key => $item) { if ($item->product_id == $product->id) { return $key; } }
		return false;
	}

	private function update_grand_total() {
		$this->grand_total = $this->sub_total;
		if ($this->discount > -1) {	$this->grand_total -= $this->discount; }
		if ($this->transport_price > -1) { $this->grand_total += $this->transport_price; }
		return $this->grand_total;
	}



	/** **************************************** end private functions ****************************************/


}

?>